// Copyright (c) 2022 Presto Labs Pte. Ltd.
// Author: hyuan

#pragma once

#include <boost/foreach.hpp>

#include "coin2/exchange/base/api_util/api_signature.h"
#include "coin2/exchange/base/api_util/json_writer.h"

class BybitSpotBaseAuth {
 public:
  virtual ~BybitSpotBaseAuth() = default;
};

class BybitSpotWsAuth : public BybitSpotBaseAuth {
 public:
  explicit BybitSpotWsAuth(const std::string& key_filepath)
      : key_(AuthKey::FromFile(key_filepath)) {
  }

  std::string CreateSignature() const {
    std::list<std::string> out;
    std::string timestamp = std::to_string(GetCurrentTimestamp() / 1000000 + 10000);
    std::string sign =
        ApiSignatureUtil::GetSignatureByHexHmacSHA256("GET/realtime" + timestamp, key_.secret_key());
    out.emplace_back(key_.access_key());
    out.emplace_back(timestamp);
    out.emplace_back(sign);

    JsonWriter writer;
    writer.PutString("op", "auth");
    writer.PutArray("args", out);
    return writer.ToJsonString();
  }

 protected:
  AuthKey key_;
};

class BybitSpotRestAuth : public BybitSpotBaseAuth, public BaseRestAuth {
 public:
  explicit BybitSpotRestAuth(
      const std::string& key_filepath,
      const std::string& host,
      const std::string& port,
      const std::string& bind_ip)
      : key_(AuthKey::FromFile(key_filepath)), host_(host), port_(port), bind_ip_(bind_ip) {
    header_.push_back(std::make_pair("Content-Type", "application/x-www-form-urlencoded"));
  }

  explicit BybitSpotRestAuth(
      const std::string& key_filepath,
      const std::string& host,
      int32_t port,
      const std::string& bind_ip)
      : key_(AuthKey::FromFile(key_filepath)), host_(host), port_(std::to_string(port)),
          bind_ip_(bind_ip) {
    header_.push_back(std::make_pair("Content-Type", "application/x-www-form-urlencoded"));
  }

  AsioHttpContext* CreateRequestByGetWithSignature(
      const std::string& url_path,
      UrlParamsBuilder* builder) override {
    std::string timestamp = std::to_string(GetCurrentTimestamp() / 1000000);
    builder->PutGet("api_key", key_.access_key());
    builder->PutGet("timestamp", timestamp);
    std::string query_string = builder->ComputeQueryString();
    query_string += "&sign=" +
        ApiSignatureUtil::GetSignatureByHexHmacSHA256(query_string, key_.secret_key());
    std::string post_body = builder->ComputePostBody();
    CHECK(post_body.empty());
    std::string target = url_path;
    if (!query_string.empty()) {
      target += "?" + query_string;
    }

    return AsioHttpContext::NewGet(host_, port_, bind_ip_, target);
  }

  AsioHttpContext* CreateRequestByPostWithSignature(
      const std::string& url_path,
      UrlParamsBuilder* builder) override {
    std::string query_string = builder->ComputeQueryString();
    CHECK(query_string.empty());
    std::string timestamp = std::to_string(GetCurrentTimestamp() / 1000000);
    builder->PutPost("api_key", key_.access_key());
    builder->PutPost("timestamp", timestamp);
    std::string post_body = builder->ComputePostBodyAsQueryString();
    std::string sign = ApiSignatureUtil::GetSignatureByHexHmacSHA256(post_body, key_.secret_key());
    post_body = ApiSignatureUtil::UrlEncode(post_body + "&sign=" + sign);
    std::string target = url_path;
    if (!query_string.empty()) {
      target += "?" + query_string;
    }

    return AsioHttpContext::NewPost(host_, port_, bind_ip_, target, header_, post_body);
  }

  AsioHttpContext* CreateRequestByDeleteWithSignature(
      const std::string& url_path,
      UrlParamsBuilder* builder) override {
    std::string timestamp = std::to_string(GetCurrentTimestamp() / 1000000);
    builder->PutGet("api_key", key_.access_key());
    builder->PutGet("timestamp", timestamp);
    std::string query_string = builder->ComputeQueryString();
    query_string += "&sign=" +
        ApiSignatureUtil::GetSignatureByHexHmacSHA256(query_string, key_.secret_key());
    std::string post_body = builder->ComputePostBody();
    CHECK(post_body.empty());
    std::string target = url_path;
    if (!query_string.empty()) {
      target += "?" + query_string;
    }

    return AsioHttpContext::NewDelete(host_, port_, bind_ip_, target, header_, "");
  }

  AsioHttpContext* CreateRequestByPutWithSignature(
      const std::string& address,
      UrlParamsBuilder* builder) override {
    return nullptr;
  }

 protected:
  AuthKey key_;
  std::string host_;
  std::string port_;
  std::string bind_ip_;
  std::vector<std::pair<std::string, std::string>> header_;
};
